from sqlalchemy import TEXT, String
from sqlalchemy.orm import Mapped, mapped_column
from app.models.base import MappedBase
from app.models.base import id_key


class CasbinRule(MappedBase):
    id: Mapped[id_key]
    ptype: Mapped[str] = mapped_column(String(255), comment="策略类型: p / g")
    v0: Mapped[str] = mapped_column(String(255), comment="角色ID/用户uuid")
    v1: Mapped[str] = mapped_column(TEXT, comment="API路径/角色名称")
    v2: Mapped[str | None] = mapped_column(String(255), comment="请求方法")
    v3: Mapped[str | None] = mapped_column(String(255))
    v4: Mapped[str | None] = mapped_column(String(255))
    v5: Mapped[str | None] = mapped_column(String(255))

    def __str__(self) -> str:
        arr = [self.ptype]
        for v in [self.v0, self.v1, self.v2, self.v3, self.v4, self.v5]:
            if v is None:
                break
            arr.append(v)
        return ", ".join(arr)

    def __repr__(self) -> str:
        return '<CasbinRule {}: "{}">'.format(self.id, str(self))
